package util;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

public class ConvertHelper {

	public static String GetRequestUnicodeString(HttpServletRequest request,
			String parametername) {
		try {
			return new String(request.getParameter(parametername).getBytes(
					"ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();

			return "";
		}
	}
	
	public static String ConvertCP1252ToUTF8(String input) {
		try {
		return new String(input.getBytes(
		"ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
		e.printStackTrace();

		return "";
		}

		}

	public static int monthsBetween(Calendar date1, Calendar date2) {
		int monthsBetween = 0;
		monthsBetween = (date1.get(Calendar.YEAR) - date2.get(Calendar.YEAR)) * 12;
		monthsBetween += date1.get(Calendar.MONTH) - date2.get(Calendar.MONTH);
		if (date1.get(Calendar.DAY_OF_MONTH) != date1
				.getActualMaximum(Calendar.DAY_OF_MONTH)
				&& date1.get(Calendar.DAY_OF_MONTH) != date1
						.getActualMaximum(Calendar.DAY_OF_MONTH)) {
			monthsBetween += ((date1.get(Calendar.DAY_OF_MONTH) - date2
					.get(Calendar.DAY_OF_MONTH)) / 31d);
		}
		return monthsBetween;
	}

	public static Date ConvertStringToDate(String dateString) {

		DateFormat formatter;
		Date date = Calendar.getInstance().getTime();
		formatter = new SimpleDateFormat("dd/mm/yyyy");

		try {
			date = (Date) formatter.parse(dateString);
			System.out.println("From " + dateString + " to " + date);
		} catch (Exception e) {
			System.out.println("Exception :" + e);
		}
		return date;
	}
	

	public static String GetNowTime()
	{
		long time = Calendar.getInstance().getTime().getTime();
		return String.valueOf(time);
	}
}
